<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<jsp:useBean id="beginTime" class="java.util.Date" />
<jsp:useBean id="endTime" class="java.util.Date" />
<jsp:setProperty name="beginTime" property="time" value="${umbrellaRuleRecharge.beginTime}"/>
<jsp:setProperty name="endTime" property="time" value="${umbrellaRuleRecharge.endTime}"/>

<div id="updateDialog" class="container-fluid">
    <div class="row">
        <form id="updateForm" method="post" class="form-horizontal" action="">
            <div class="form-group">
                <label class="col-sm-4 control-label" for="name">
                    <span class="help-inline"><font color="red">*</font> </span>
                    名称：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="name" name="name" placeholder="名称" value="${umbrellaRuleRecharge.name}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="price">
                    <span class="help-inline"><font color="red">*</font> </span>
                    面额(元)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="price" name="price" placeholder="面额" value="${umbrellaRuleRecharge.price}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="saleprice">
                    <span class="help-inline"><font color="red">*</font> </span>
                    售价(元)：</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="saleprice" name="saleprice" placeholder="售价" value="${umbrellaRuleRecharge.saleprice}" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="beginTime">
                    <span class="help-inline"><font color="red">*</font> </span>
                    开始时间：</label>
                <div class="col-sm-5">
                        <input type="text" id="beginTime"  class="form-control" data-date-format="yyyy-mm-dd" placeholder="开始时间" value="<fmt:formatDate value="${beginTime}" pattern="yyyy-MM-dd" />">
                        <input type="text" hidden name="beginTime" value="${umbrellaRuleRecharge.beginTime}">

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="endTime">
                    <span class="help-inline"><font color="red">*</font> </span>
                    结束时间：</label>
                <div class="col-sm-5">
                        <input type="text" id="endTime" class="form-control" data-date-format="yyyy-mm-dd" placeholder="结束时间" value="<fmt:formatDate value="${endTime}" pattern="yyyy-MM-dd" />">
                        <input type="text" hidden name="endTime" value="${umbrellaRuleRecharge.endTime}">

                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="status">
                    <span class="help-inline"><font color="red">*</font> </span>
                    状态：</label>
                <div class="col-sm-5">
                        <select id="status" name="status" class="form-control">
                            <option value="1" <c:if test="${umbrellaRuleRecharge.status==1}">selected="selected"</c:if>>启用</option>
                            <option value="0" <c:if test="${umbrellaRuleRecharge.status==0}">selected="selected"</c:if>>>禁止</option>
                        </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" for="description">描述：</label>
                <div class="col-sm-5">
                    <textarea class="form-control" id="description" name="description" placeholder="描述" value="${umbrellaRuleRecharge.description}"></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-9 col-sm-offset-4">
                    <button type="button" class="btn btn-primary" onclick="createSubmit();">
                        保存
                    </button>
                    <button type="button" class="btn btn-default"
                            onclick="updateDialog.close();">取消
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    function validateForm(){
        return $( "#updateForm" ).validate( {
            rules: {
                name: {
                    required: true,
                    maxlength: 50
                },
                price: {
                    required: true
                },
                saleprice: {
                    required: true
                },
                beginTime: {
                    required: true
                },
                endTime: {
                    required: true
                },
                status: {
                    required: true
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: {
                    required: "该项为必填项。",
                    maxlength: "长度超出50个字符。"
                },
                price: {
                    required: "该项为必填项。",
                },
                saleprice: {
                    required: "该项为必填项。",
                },
                beginTime: {
                    required: "该项为必填项。",
                },
                endTime: {
                    required: "该项为必填项。",
                },
                status: {
                    required: "该项为必填项。",
                },
                description: {
                    maxlength: "长度超出500个字符"
                }
            },
            errorElement: "em",
            errorPlacement: function ( error, element ) {
                // Add the `help-block` class to the error element
                error.addClass( "help-block" );

                if ( element.prop( "type" ) === "checkbox" ) {
                    error.insertAfter( element.parent( "label" ) );
                } else {
                    error.insertAfter( element );
                }
            },
            highlight: function ( element, errorClass, validClass ) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
            },
            unhighlight: function (element, errorClass, validClass) {
                $( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
            }
        } );
    }
    //注册表单验证
    $(validateForm());

    $(function(){
        $('#beginTime,#endTime').datepicker({
//            format: 'yyyy-mm-dd'
        });
    });
    function createSubmit() {
        if(validateForm().form()){
            if($("#beginTime").val())
                $("input[name='beginTime']").val(getLongFromatDateStr($("#beginTime").val()));
            if($("#endTime").val())
                $("input[name='endTime']").val(getLongFromatDateStr($("#endTime").val()));
            $.ajax({dataType: 'json',
                type: 'post',
                url: '${basePath}/manage/recharge/update/${umbrellaRuleRecharge.id}',
                data: $('#updateForm').serialize(),
                success: function(result) {
                    if (result.code != 1) {
                        if (result.data instanceof Array) {
                            $.each(result.data, function(index, value) {
                                infoDialog(value.errorMsg);
                            });
                        } else {
                            infoDialog(result.data);
                        }
                    } else {
                        updateDialog.close();
                        $table.bootstrapTable('refresh');
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    infoDialog(textStatus);
                }
            });
        }
    }
</script>